package redis

import (
	"errors"
	"fmt"
	"gitee.com/xuxinzhi2206/corehubs/nacos"
	"gitee.com/xuxinzhi2206/corehubs/types"
	"github.com/go-redis/redis"
	"gopkg.in/yaml.v2"
)

func WriteRedisDB(head func(client *redis.Client)) {
	info, err := nacos.GetNaCosInfo()
	fmt.Println("nacos配置信息：：：", info)
	var res types.ServerConfig
	_ = yaml.Unmarshal([]byte(info), &res.Redis)

	client := redis.NewClient(&redis.Options{
		Addr:     fmt.Sprintf("%s:%d", res.Redis.Host, res.Redis.Port),
		Password: "", // no password set
		DB:       0,  // use default DB
	})
	pong, err := client.Ping().Result()
	if err != nil {
		panic(errors.New("缓存链接失败"))
	}
	// Output: PONG <nil>
	fmt.Println("缓存连接成功", pong)
	defer func() {
		client.Close()
	}()
	head(client)
}
